package com.sangsang.ck.methods;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.methods.SelectBatchByIds;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * 批量查询当前主键在数据库中已经存在的数据
 *
 * @author liutangqi
 * @date 2023/12/25 16:31
 * @see SelectBatchByIds  参考的这个
 */
public class SelectExistIdByIds extends AbstractMethod {

    private static final String METHOD_NAME = "selectExistIdByIds";
    private static final String sql = "<script>SELECT %s FROM %s WHERE %s IN (%s) %s</script>";

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, String.format(sql, tableInfo.getKeyColumn(), tableInfo.getTableName(), tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach("#{item}", "coll", (String) null, "item", ","), tableInfo.getLogicDeleteSql(true, true)), Object.class);
        return this.addSelectMappedStatementForOther(mapperClass, METHOD_NAME, sqlSource, tableInfo.getKeyType());
    }
}
